using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameForKids
{

    public class LoadingScreen : GameScreen
    {
        // nen bau troi
        Texture2D pixel;
        // nen dam may
        Texture2D background;
        // thanh loader
        Texture2D loadingbar;
        // mat troi
        Texture2D thesun;
        // tia nang
        Texture2D sunLine;
        List<Texture2D> list_sunline;

        SpriteFont font;
        InputAction _quit_action;
        // vi tri thanh loader
        Vector2 bar_location;
        // vi tri cua so %
        Vector2 phantram_location;
        // vi tri hien tai cua mat troi
        Vector2 first_sun_pos = new Vector2(240,350);
        // tam cua mat troi
        Vector2 tam_xoay = new Vector2(240, 160);

        int dodai = 0;
        int phantram = 0;

        Random rand;
        Animation loaderAnimation;
        float rotate;
        int alpha = 255;

        SoundEffect sound;
        Boolean isplayed = false;

        public LoadingScreen()
        {
           
        }

        public override void Activate(bool instancePreserved)
        {
            rand = new Random();

            bar_location = new Vector2(180, 450);

            phantram_location = bar_location + new Vector2(5, -2);

            pixel = new Texture2D(ScreenManager.GraphicsDevice, 1, 1);
            pixel.SetData(new[] { new Color(109,188,255) });

            background = ScreenManager.Game.Content.Load<Texture2D>("Loading/Images/loading_background");
            loadingbar = ScreenManager.Game.Content.Load<Texture2D>("Loading/Images/loader_bar");
            thesun = ScreenManager.Game.Content.Load<Texture2D>("Loading/Images/sun");
            sunLine = ScreenManager.Game.Content.Load<Texture2D>("Loading/Images/sun_line");

            sound = ScreenManager.Game.Content.Load<SoundEffect>("Loading/Sounds/sound_intro");

            list_sunline = new List<Texture2D>();
            for (int i = 0; i < 8; i++)
            {
                list_sunline.Add(sunLine);
            }

            font = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/LoadFont");
            _quit_action = new InputAction(new Buttons[] { Buttons.Back }, null, true);
            loaderAnimation = new Animation(loadingbar, new Point(dodai, 19), new Point(1, 5));
            loaderAnimation.PlayFromFrameIndex(0);
            loaderAnimation.Active();
            rotate = 0f;

            base.Activate(instancePreserved);
        }

        public override void Unload()
        {
            ScreenManager.Game.Content.Unload();
        }

        // trong luc loading duoc quyen thoat
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            PlayerIndex player;
            if (_quit_action.Evaluate(input, ControllingPlayer, out player))
            {
                ScreenManager.Game.Exit();
            }
            base.HandleInput(gameTime, input);
        }

        // delay thanh loader
        private float TimeTick = 0f;
        // delay truoc khi bat dau load
        private float timebefor = 0f;
        // sau khi 100% cung delay
        private float timeDelayLoaderbar = 0f;

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.Milliseconds;
            
            if (timebefor < 500)
            {
                timebefor += elapsed;

            }
   


            // cap nhat do dai
            TimeTick += elapsed;
            if (phantram < 100 && TimeTick >=100 && timebefor >= 500 )
            {

                if (phantram > 15 && !isplayed && GameForKids.ISMUSIC)
                {
                    sound.Play();
                    isplayed = true;
                }

                int temp = rand.Next(0, 30) ;
                dodai += temp;
                phantram = (int)(((float)dodai / 500f)*100);
                if (phantram > 100)
                    phantram = 100;
                phantram_location.X += temp;
                TimeTick = 0;
            }

            // chuyen qua main menu
            if (phantram >= 100 && TimeTick > 1000)
            {
                alpha -= 20;
                if (alpha < 10)
                {
                    TimeTick = 0;
                    GameForKids.gameState = GameState.Login;
                    ScreenManager.Game.Content.Unload();
                    ExitScreen();
                    ScreenManager.AddScreen(new LoginScreen(), null);
                }
            }

            // cap nhat loader bar
            timeDelayLoaderbar += elapsed;
            if (timeDelayLoaderbar > 100)
            {
                loaderAnimation.updateFrameWidth(new Point(dodai, 19));
                loaderAnimation.Update();
                timeDelayLoaderbar = 0;

            }
            // vi tri mat troi
            if (first_sun_pos.Y > tam_xoay.Y)
            {
                first_sun_pos.Y -= 2;
            }
            rotate += (float)Math.PI / 50;
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spritebatch = ScreenManager.SpriteBatch;
            spritebatch.Begin();
            spritebatch.Draw(pixel, new Rectangle(0, 0, GameForKids.WIDTH, GameForKids.HEIGHT), new Color(alpha, alpha, alpha));
            //ve tia nang
            for (int i = 0; i < 8; i++)
            {
                spritebatch.Draw(list_sunline[i],
                    new Vector2(first_sun_pos.X, first_sun_pos.Y),
                    null,
                    new Color(alpha,alpha,alpha),
                    rotate+i*(float)Math.PI/4,
                    new Vector2(0, sunLine.Height / 2),
                    1.0f,
                    SpriteEffects.None,
                    0f);
            }

            //ve mat troi

            spritebatch.Draw(thesun, new Rectangle(
                (int)first_sun_pos.X - thesun.Width/2,
                (int)first_sun_pos.Y - thesun.Height/2,
                thesun.Width,
                thesun.Height),
                new Color(alpha, alpha, alpha));

            // ve nen may
            spritebatch.Draw(background, new Rectangle(0, 0, 800, 480), new Color(alpha, alpha, alpha));

            spritebatch.DrawString(font, "Loading", new Vector2(190, 400), new Color(alpha, alpha, alpha));

            spritebatch.DrawString(font, "V 1.0.2", new Vector2(700, 10), new Color(alpha, alpha, alpha),
                0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 0);

            // ve loader bar
            loaderAnimation.Draw(spritebatch, bar_location, 1.0f, SpriteEffects.None, alpha);

            spritebatch.DrawString(font, phantram + "%", phantram_location, new Color(alpha, alpha, alpha),
                 0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 0);
            

            //xem RAM
           /* var appCurrentMemoryUsage = DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
            var appPeakMemoryUsage = DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");
            spritebatch.DrawString(font, "Current RAM: " + appCurrentMemoryUsage.ToString(), new Vector2(10, 10), Color.White);
            spritebatch.DrawString(font, "Total RAM: " + appPeakMemoryUsage.ToString(), new Vector2(10, 50), Color.White);
            */
            spritebatch.End();
            base.Draw(gameTime);
        }
    }
}
